#!/usr/bin/perl

require "./quotamin-lib.pl";
&ReadParse();

my ($status, $h_unit, $s_unit, $h_input_num, $s_input_num, $error_msg);
my ($dir, $lable, $sblocks, $hblocks);
my ($logminor, $logpriority, $event, $formatstr);

$dir = $in{"checkeddir"};
$lable = $in{"lable"};
$status = $in{"limitstatus"};
$h_unit = $in{"h_unit"};
$s_unit = $in{"s_unit"};
$h_input_num = $in{"h_input_num"};
$s_input_num = $in{"s_input_num"};
$error_msg = "";

if($status eq "ok")
{
	$error_msg = &check_input_num($h_input_num, $h_unit);
	unless($error_msg)
	{
		$error_msg = &check_input_num($s_input_num, $s_unit);
	}

	if($error_msg)
	{
		$logminor= $text{'module_name'};
		$logpriority=$text{'naslog_warning'};
		$event = $text{'quotamin_naslog_event_savedefault_failed_by_num_err_format'};
		$formatstr = sprintf $text{'quotamin_naslog_savedefault_failed_by_num_err_format'}, $lable;	
	}

	unless($error_msg)
	{
		$sblocks = &KMG_str_to_blocks($s_input_num, $s_unit);
		$hblocks = &KMG_str_to_blocks($h_input_num, $h_unit);
		$error_msg = &check_blocks_input($hblocks, $sblocks);
		
		if($error_msg)
		{
			$logminor= $text{'module_name'};
			$logpriority=$text{'naslog_warning'};
			$event = $text{'quotamin_naslog_event_savedefault_failed_by_sblock_bigto_hblock'};
			$formatstr = sprintf $text{'quotamin_naslog_savedefault_failed_by_sblock_bigto_hblock'}, $lable;
		}
	}

	unless($error_msg)
	{
		$sblocks = &KMG_str_to_blocks($s_input_num, $s_unit);
		$hblocks = &KMG_str_to_blocks($h_input_num, $h_unit);

		$sblocks = sprintf("%.0f", $sblocks);
		$hblocks = sprintf("%.0f", $hblocks);

		if($hblocks <= 4)
		{			
			$hblocks = 4;
		}

		if($sblocks <= 4)
		{	
			$sblocks = 4;
		}
	}
}
else
{
	$sblocks = 0;
	$hblocks = 0;
}

if($error_msg)
{
	if($event)
	{
		&nas_syslog($logminor, $logpriority, $event, $formatstr);
	}

	&error_setup($text{"error_title_save_default_quota"});
	&error($error_msg);
}
else
{

	#$sfiles = &get_largest_inodes_with_fs($dir);
	$sfiles = 0;
	&update_usrdef_quota($dir, $sblocks, $hblocks, $sfiles, 0);
	
	
	$logminor= $text{'module_name'};
	$logpriority=$text{'naslog_info'};
	$event = $text{'quotamin_naslog_event_savedefault_success'};
	if(($hblocks == 0) && ($sblocks == 0))
	{
		$formatstr = sprintf $text{'quotamin_naslog_savedefault_success'}, $lable, "NO_LIMIT", "NO_LIMIT";
	}
	else
	{
		$formatstr = sprintf $text{'quotamin_naslog_savedefault_success'}, $lable, $h_input_num.$h_unit, $s_input_num.$s_unit;
	}
	
	&nas_syslog($logminor, $logpriority, $event, $formatstr);

	&redirect("index.cgi");
	#&footer("/quotamin/", $text{'index'});
}
